package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
public class RecommendUserApiImpl   implements RecommendUserApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public RecommendUser findMaxScoreUser(Long userId) {
        // where toUserId = 用户id and date = 当天日期
        Criteria criteria = Criteria.where("toUserId").is(userId)
                .and("date").is(new DateTime().toString("yyyy/MM/dd"));

        // 按照score值降序排列，查询第一条
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageResult findRecommendByUser(Long userId, int page, int pagesize) {
        // 查询数据列表
        Query query = new Query();
        //1. 添加查询条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("date").is(new DateTime().toString("yyyy/MM/dd"));
        query.addCriteria(criteria);
        //2. 设置排序信息
        query.with(Sort.by(Sort.Order.desc("score")));
        //3. 设置分页信息
        query.with(PageRequest.of(page-1, pagesize));

        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        // 查询数量
        long count = mongoTemplate.count(query, RecommendUser.class);

        // 封装分页结果
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        pageResult.setCounts((int) count);
        pageResult.setPages((int) Math.ceil(count*1.0/pagesize));
        pageResult.setItems(list);
        return pageResult;
    }

    @Override
    public Integer findRecommendScore(Long toUserId, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("toUserId").is(userId));
        query.limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser != null) {
            return recommendUser.getScore().intValue();
        }
        return null;
    }

    @Override
    public List<RecommendUser> findRecommendList(Long toUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(toUserId));
        query.with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        return list;
    }

}
